package com.wangsd.web.common.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wangsd.common.basicbean.Result;
import com.wangsd.web.common.service.UploadFileService;
import com.wechat.pay.contrib.apache.httpclient.util.AesUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;

/**
 * 文件上传控制器
 */

@RestController
@RequestMapping("/common/upload")
@Api(value = "upload", tags = "文件通用异步上传")
@Slf4j
public class UploadController {

    @Autowired
    UploadFileService uploadFileService;

    @RequestMapping("/coupon")
    public void coupon(@RequestBody String body) {
        log.debug("代金券核销回调==" + body);
        JSONObject obj = JSON.parseObject(body).getJSONObject("resource");
        String associated_data = obj.getString("associated_data");
        String nonce = obj.getString("nonce");
        String ciphertext = obj.getString("ciphertext");
        try {
            AesUtil decryptor = new AesUtil("6e900cd3bf145a7ac231b6f570271a47".getBytes("utf-8"));
            String str = decryptor.decryptToString(associated_data.replaceAll("\"", "").getBytes(),
                    nonce.replaceAll("\"", "").getBytes(),
                    ciphertext.replaceAll("\"", ""));
            log.debug("代金券核销回调2==" + str);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 上传图片
     * @param file
     * @return
     */
    @ApiOperation(value = "上传图片", notes = "上传图片", httpMethod = "POST")
    @RequestMapping(value = "uploadImg", method = RequestMethod.POST)
    public Object uploadImg(@ApiParam(value = "图片文件", required = true) @PathVariable("file") MultipartFile file) {
        return uploadFileService.uploadImg(file);
    }

    /**
     * 上传文件
     * @param file
     * @return
     */
    @ApiOperation(value = "上传文件", notes = "上传文件", httpMethod = "POST")
    @RequestMapping(value = "uploadFile", method = RequestMethod.POST)
    public Object uploadFile(@ApiParam(value = "文件", required = true) @PathVariable("file") MultipartFile file) {
        return uploadFileService.uploadFile(file);
    }

    /**
     * 上传证书
     * @param file
     * @return
     */
    @ApiOperation(value = "上传证书", notes = "上传证书", httpMethod = "POST")
    @RequestMapping(value = "uploadCertificateFile", method = RequestMethod.POST)
    public Object uploadCertificateFile(@ApiParam(value = "文件", required = true) @PathVariable("file") MultipartFile file) {
        return uploadFileService.uploadCertificateFile(file);
    }

    /**
     * 删除图片
     * @param name
     * @return
     */
    @ApiOperation(value = "删除图片", notes = "删除图片", httpMethod = "GET")
    @RequestMapping(value = "/deletePic", method = RequestMethod.GET)
    public Result deletePic(@ApiParam(value = "文件名", required = true) @RequestParam("name") String name) {
        uploadFileService.deleteImg(name);
        return Result.success();
    }

    /**
     * 删除文件
     * @param name
     * @return
     */
    @ApiOperation(value = "删除文件", notes = "删除文件", httpMethod = "GET")
    @RequestMapping(value = "/deleteFile", method = RequestMethod.GET)
    public Result deleteFile(@ApiParam(value = "文件名", required = true) @RequestParam("name") String name) {
        uploadFileService.deleteFile(name);
        return Result.success();
    }
}
